import { quest } from "../../utils/http";

// pages/updateUniversity/updateUniversity.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: "",
    item: {},
    content: "",
    alter: "",
    university: [],
    myUniversity: {
      id: 2,
      university: "湖南农业大学",
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let { page } = options;
    let item;
    switch (page) {
      case "university":
        item = options.item;
        item = JSON.parse(item);
        this.setData({ item });
        break;
      case "delivery":
        item = options.item;
        item = JSON.parse(item);
        this.setData({ item });
        break;
      case "advert":
        item = options.item;
        item = JSON.parse(item);
        this.setData({ item });
        break;
    }

    let university = await quest({
      url: "university/list?size=99",
    });
    // console.log(university);
    this.setData({
      page,
      university: university.data.items,
    });
  },
  valueChange(e) {
    console.log(e.detail.value);
    this.setData({
      content: e.detail.value,
      alert: "",
    });
  },
  async onSubmit() {
    let { page, content } = this.data;
    if (content.length == 0) {
      this.setData({
        alert: "请输入学校名称在提交",
      });
      return;
    }
    let data;
    let { item } = this.data;
    switch (page) {
      case "university":
        (item.university = content),
          (data = await quest({
            url: "university/update",
            method: "post",
            data: {
              ...item,
            },
          }));
        break;
      case "delivery":
        (item.deliveryAddr = content),
          (data = await quest({
            url: "delivery/update",
            method: "post",
            data: {
              ...item,
            },
          }));
        break;
      case "advert":
        (item.content = content),
          (data = await quest({
            url: "advert",
            method: "post",
            data: {
              ...item,
            },
          }));
        break;
      case "insertUniversity":
        data = await quest({
          url: "university/put",
          method: "put",
          data: {
            university: content,
          },
        });
        break;
      case "insertDelivery":
        let { myUniversity } = this.data;
        data = await quest({
          url: "delivery/put",
          method: "put",
          data: {
            deliveryAddr: content,
            universityId: myUniversity.id,
          },
        });
        break;
      case "insertAdvert":
        data = await quest({
          url: "advert",
          method: "put",
          data: {
            content: content,
          },
        });
        console.log(data);
        break;
    }
    if (data.code == 200) {
      console.log(data);
      wx.showModal({
        title: "提示",
        content: "成功",
        success(res) {
          if (res.confirm) {
            // wx.navigateBack;
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    }
  },
  onReset() {
    this.setData({
      content: "",
    });
  },
  bindDeliveryChange(e) {
    let myUniversity = this.data.university[e.detail.value];
    this.setData({
      myUniversity,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
